package com.sizu.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sizu.common.basicdto.TSupplierDto;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TSupplier;
import com.sizu.service.ITSupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵玉玺
 * @since 2020-03-15
 */
@RestController
@RequestMapping("/basic/tSupplier")
public class TSupplierController {

    @Resource
    private ITSupplierService itSupplierService;

    /**
     * 查询供应商列表
     * @param tSupplierDto
     * @return
     */
    @RequestMapping("/list")
    public ResultEntity tSupplierList(TSupplierDto tSupplierDto){
        PageHelper.startPage(tSupplierDto.getPageNum(),tSupplierDto.getPageSize());
        Wrapper<TSupplier> wrapper=new EntityWrapper<>();
        wrapper.eq("enable",1);
        if(null!=tSupplierDto.getName()&&!"".equals(tSupplierDto.getName())){
            wrapper.like("name",tSupplierDto.getName());
        }
        List<TSupplier> tSuppliers = itSupplierService.selectList(wrapper);
        PageInfo<TSupplier> pageInfo=new PageInfo<>(tSuppliers);
        return ResultEntity.ok("查询成功",pageInfo);
    }

    /**
     * 添加供应商信息
     * @param tSupplierDto
     * @return
     */
    @RequestMapping("/add")
    public ResultEntity tSupplierAdd(TSupplierDto tSupplierDto){
        TSupplier tSupplier=new TSupplier();
        BeanUtils.copyProperties(tSupplierDto,tSupplier);
        itSupplierService.insert(tSupplier);
        return ResultEntity.ok("添加成功");
    }

    /**
     * 修改供应商信息
     * @param tSupplierDto
     * @return
     */
    @RequestMapping("/up")
    public ResultEntity tSupplierUp(TSupplierDto tSupplierDto){
        TSupplier tSupplier=new TSupplier();
        BeanUtils.copyProperties(tSupplierDto,tSupplier);
        itSupplierService.updateById(tSupplier);
        return ResultEntity.ok("修改成功");
    }

    /**
     * 删除方法带批删
     * @param tSupplierDto
     * @return
     */
    @RequestMapping("/del")
    public ResultEntity tSupplierDel(TSupplierDto tSupplierDto){
        for (Integer id : tSupplierDto.getIds()) {
            TSupplier tSupplier=new TSupplier();
            tSupplier.setId(id);
            tSupplier.setEnable(0);
            itSupplierService.updateById(tSupplier);
        }
        return ResultEntity.ok("删除成功");
    }

}
